package com.atguigu.gmall.index.config;

import com.atguigu.gmall.common.bean.ResponseVo;
import com.atguigu.gmall.index.feign.GmallPmsClient;
import com.atguigu.gmall.pms.entity.CategoryEntity;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
public class BloomFilterConfig {
    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private GmallPmsClient gmallPmsClient;

    private static final String KEY_PREFIX = "index:cates:";
    @Bean
    public RBloomFilter bloomFilter(){
        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter("index:bl");
        bloomFilter.tryInit(2000,0.03);
        //放入一级分类的id
        ResponseVo<List<CategoryEntity>> listResponseVo = gmallPmsClient.listResponseVoById(0l);
        List<CategoryEntity> categoryEntities = listResponseVo.getData();
        //遍历一级分类，把一级分类id放入布隆过滤器
        if (CollectionUtils.isNotEmpty(categoryEntities)){
         categoryEntities.forEach(categoryEntity -> {
             bloomFilter.add(KEY_PREFIX + categoryEntity.getId());
         });
        }
        //广告id
        return bloomFilter;
    }
}
